package com.buli.controller;

import com.buli.pojo.User;
import com.buli.service.UserService;
import com.buli.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 获取用户详情
     */
    @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody Result<User> getUserById(@PathVariable Integer id) {
        if (id == null || id <= 0) {
            return Result.fail("ID 不能为空且必须大于 0");
        }

        User user = userService.getUserById(id);
        if (user == null) {
            return Result.fail("用户不存在");
        }

        return Result.success(user);
    }

    /**
     * 获取用户列表
     */
    @GetMapping(value = "/getAll", produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody Result<List<User>> getUserList() {
        List<User> userList = userService.getUserList();
        return Result.success(userList);
    }
}